package com.zkca.xf.ui.activity.personprotect;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zkca.xf.ConstantsXF;
import com.zkca.xf.Constants_SP;
import com.zkca.xf.MyApplication;
import com.zkca.xf.R;
import com.zkca.xf.base.BaseTitleActivity;
import com.zkca.xf.model.javabean.FaultJobLBean;
import com.zkca.xf.model.javabean.JobLBean;
import com.zkca.xf.model.javabean.uploadbean.RequestInspect;
import com.zkca.xf.presenter.FaultSubitpresenter;
import com.zkca.xf.utils.LogUtil;
import com.zkca.xf.utils.SPUtil;
import com.zkca.xf.utils.StringUtils;
import com.zkca.xf.utils.TimeUtils;
import com.zkca.xf.utils.ToastUtils;
import com.zkca.xf.view.FaultJobSubmitView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FaultJobSubmitActivity extends BaseTitleActivity implements FaultJobSubmitView{

    @BindView(R.id.faultnum)
    TextView faultnum;
    @BindView(R.id.devid)
    TextView devid;
    @BindView(R.id.devname)
    TextView devname;
    @BindView(R.id.installposition)
    TextView installposition;
    @BindView(R.id.xunjianuser)
    TextView xunjianuser;
    @BindView(R.id.xunjiantime)
    TextView xunjiantime;
    @BindView(R.id.commun)
    TextView commun;
    @BindView(R.id.imagadd)
    ImageView imagadd;
    @BindView(R.id.imaglinear)
    LinearLayout imaglinear;
    @BindView(R.id.imagandtext)
    LinearLayout imagandtext;
    @BindView(R.id.button_anquan)
    RadioButton buttonAnquan;
    @BindView(R.id.button_guzhang)
    RadioButton buttonGuzhang;
    @BindView(R.id.devstutesgroup)
    RadioGroup devstutesgroup;
    @BindView(R.id.hanldmsg)
    EditText hanldmsg;
    @BindView(R.id.beganup)
    Button beganup;

    FaultJobLBean.DataBean dataBean;
    String devImage4;
    String name3;
    int faultId;

    private int type1=-1;
    private Dialog loadingDialog;
    private RequestInspect requestInspect;
    private FaultSubitpresenter faultSubitpresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fault_job_submit);
        ButterKnife.bind(this);
        initViewData();
        setTitleText("故障处理提交");
        setleftTextVisibility(true);
        setleftImgVisibility(true);
        loadingDialog=new Dialog(mContext);
        LayoutInflater inflater = LayoutInflater.from(this);
        View dialogView     = inflater.inflate(R.layout.dialogloading, null);
        //将自定义布局设置进去
        loadingDialog.setContentView(dialogView);
        loadingDialog.setCanceledOnTouchOutside(false);

        faultSubitpresenter=new FaultSubitpresenter(this,this);
        //initViewData();
        devstutesgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId();
                RadioButton choise = (RadioButton) findViewById(checkedRadioButtonId);
                // 获取这个RadioButton的text内容

                String s = choise.getText().toString();
                if ("是".equals(s)){
                    type1=0;
                }else if ("否".equals(s)){
                    type1=1;
                }

            }
        });
        beganup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                upFalut();
            }
        });
    }

    private void upFalut() {
        if (type1==-1){
            ToastUtils.show(mContext,"请选择是否解决");
            return;
        }
        loadingDialog.show();
        Map map=new HashMap();
        map.put("type1",type1);
        map.put("faultId",faultId);
        if (!StringUtils.isEmpty(hanldmsg.getText())){
            map.put("handleDesc",hanldmsg.getText()+"");
        }
        requestInspect = new RequestInspect();
        requestInspect.setParam(map);
        requestInspect.setAccessId(SPUtil.getString(this, Constants_SP.ACCESSID));
        requestInspect.setAction(7);
        requestInspect.setTimeStamp(TimeUtils.getTime());
        requestInspect.setSign(MyApplication.getInstance().generateDvSign(requestInspect));
        faultSubitpresenter.FaultSubit(requestInspect);
       // jobCheckpresenter.submitJob(requestInspect);
    }

    private void initViewData() {
        Intent intent = getIntent();
        dataBean = (FaultJobLBean.DataBean) intent.getSerializableExtra("jobbean");
        devImage4 = (String) dataBean.getDevImage4();
//                LogUtil.e(dataBean.getName3()+"图片：",devImage4+"");
        name3 = dataBean.getName3();//故障编号
        int handleResult = dataBean.getHandleResult();//执行状态  1 为执行 2 已执行
        String handleUname = dataBean.getHandleUname();//指派用户
        String devName = dataBean.getDevName();//设备名称
        String installPosition = dataBean.getInstallPosition();//设备地点
        String name1 = dataBean.getName1();//问题描述
        faultId= dataBean.getFaultId();//故障ID
        long createTime = dataBean.getCreateTime();
        String formatDate2 = TimeUtils.getFormatDate2(createTime);

        faultnum.setText(name3);
        devid.setText(dataBean.getDevId());
        devname.setText(devName);
        installposition.setText(installPosition);
        xunjianuser.setText(dataBean.getUserName());
        xunjiantime.setText(formatDate2);
        boolean contains = name1.contains("<br/>");
        if (contains){
            name1= name1.replace("<br/>","注：");
        }
        commun.setText(name1);

        commun.setText(name1);
        LogUtil.e("Image4==",ConstantsXF.URL_SERVER_CE + devImage4);
        Glide.with(this).load(ConstantsXF.URL_SERVER_CE + devImage4).asBitmap().placeholder(R.mipmap.zwf).error(R.mipmap.zwf).into(imagadd);
    }

    @Override
    public void showProgressDialog() {

    }

    @Override
    public void hideProgressDialog() {

    }

    @Override
    public void showError(String msg) {
        ToastUtils.show(mContext,msg);
        Intent intent = new Intent(mContext, FaultJob_LActivity.class);
        startActivity(intent);
        loadingDialog.dismiss();
        finish();
    }

    @Override
    public void FaultSubmit(JobLBean s) {
        //ToastUtils.show(mContext,s.getInfo());
        if (9==s.getResult()){
            ToastUtils.show(mContext,"您不是该维保任务指定人，请任务指定人员完成该任务");
        }else if (0==s.getResult()){
            ToastUtils.show(mContext,s.getInfo());
        }
        Intent intent = new Intent(mContext, FaultJob_LActivity.class);
        startActivity(intent);
        loadingDialog.dismiss();
        finish();
    }
}
